'use strict';

define(['angular', 'moment', 'angular-ui-router', '../../../common/utils/service', 'angular-material'], function (angular, moment) {
	var module = angular.module('memberOrder', ['ui.router', 'dataService', 'memberAccount', 'ngMaterial']);
	module.config(['$stateProvider', function ($stateProvider) {
		$stateProvider
			.state("member.order", {
				url: '/order',
				views: {
					"@": {
						controller: 'MemberOrderCtrl',
						templateUrl: '/mstore/member/order/order.html'
					}
				}
			})
		$stateProvider
			.state("member.oldOrder", {
				url: '/member-order',
				views: {
					"@": {
						controller: 'MemberOldOrderCtrl'
					}
				}
			})
		$stateProvider
			.state("member.order.detail", {
				url: '/detail/:id',
				views: {
					"@": {
						controller: 'MemberOrderDetailCtrl',
						templateUrl: '/mstore/member/order/detail.html'
					}
				}
			})
	}]);

	//TODO 以后会删除
	module.controller('MemberOldOrderCtrl', ['$state', function ($state) {
		$state.transitionTo('member.order')
	}])
	module.controller('MemberOrderCtrl', ['$rootScope', '$state', '$scope', 'dataService', function ($rootScope, $state, $scope, dataService) {
		$rootScope.pageTitle = "订单列表";

		$scope.memberBound(function () {
			var month = moment().format("YYYY-MM");
			dataService.getMemberOrders(sessionStorage.memberId, month, function (err, orders) {
				if (err) return;
				$scope.ordersData = orders;
				$scope.ordersData.orders = dataService.generateMonthData(month, orders.orders, "orders", 'closetime');
			})
		}, {force: true})


	}]);
	module.controller('MemberOrderDetailCtrl', ['$state', '$scope', 'dataService', '$rootScope', '$stateParams', function ($state, $scope, dataService, $rootScope, $stateParams) {
		if (!$stateParams.id) {
			return $state.transitionTo("member.order");
		}
		$rootScope.pageTitle = "订单详情";
		$scope.goBack = function () {
			window.history.back();
		}
		dataService.getOrderInfo($stateParams.id, function (err, orderInfo) {
			if (err) return;
			$scope.orderInfo = orderInfo;
		})

	}]);
});
